﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace Acme.BookStore2.EntityFrameworkCore
{
    public class VehicleInspectionRecordRepository : EfCoreRepository<BookStore2DbContext, VehicleInspectionRecord, Guid>,
        IVehicleInspectionRecordRepository
    {
        public VehicleInspectionRecordRepository(IDbContextProvider<BookStore2DbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        public async Task<List<VehicleInspectionRecord>> GetPagedListAsync(int skipCount, int maxResultCount, string sorting, Guid? vehicleId = null,
            bool includeDetails = false, CancellationToken cancellationToken = default)
        {
            return await DbSet
                .WhereIf(vehicleId.HasValue, v => v.VehicleId == vehicleId)
                .OrderBy(sorting ?? nameof(VehicleInspectionRecord.CreationTime) + " desc")
                .PageBy(skipCount, maxResultCount)
                .ToListAsync(GetCancellationToken(cancellationToken));
        }

        public async Task<long> GetCountAsync(Guid? vehicleId = null, CancellationToken cancellationToken = default)
        {
            return await this
                .WhereIf(vehicleId.HasValue, v => v.VehicleId == vehicleId)
                .LongCountAsync(GetCancellationToken(cancellationToken));
        }
    }
}